<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="userinfoId">
        <el-select v-model="queryParams.userinfoId" filterable placeholder="请选择" clearable>
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="parseInt(item.userId)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预计转正时间" label-width="auto">
        <el-date-picker
          v-model="daterangePlanTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-select v-model="queryParams.education" placeholder="请选择学历" clearable size="small">
          <el-option
            v-for="dict in dict.type.personnel_education"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="微信号" prop="chatNum">
        <el-input
          v-model="queryParams.chatNum"
          placeholder="请输入微信号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['personnel:userinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['personnel:userinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['personnel:userinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['personnel:userinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userinfoList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="user.userName" />
      <el-table-column label="预计转正时间" align="center" prop="planTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idCard" width="90"/>
      <el-table-column label="身份证地址" align="center" prop="idAddr" width="90"/>
      <el-table-column label="现居住地址" align="center" prop="liveAddr" width="90"/>
      <el-table-column label="出生年月" align="center" prop="birthday" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="毕业院校" align="center" prop="school" />
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.personnel_education" :value="scope.row.education"/>
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" prop="profession" />
      <el-table-column label="婚姻状况" align="center" prop="marry">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.personnel_userinfo_marry" :value="scope.row.marry"/>
        </template>
      </el-table-column>
      <el-table-column label="紧急联系人" align="center" prop="ice" />
      <el-table-column label="联系人关系" align="center" prop="iceRelat" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.personnel_userinfo_marry" :value="scope.row.iceRelat"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人联系方式" align="center" prop="iceNum" width=120"/>
      <el-table-column label="对公司的看重" align="center" prop="attitude" width="120"/>
      <el-table-column label="职业规划" align="center" prop="planning" />
      <el-table-column label="微信号" align="center" prop="chatNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['personnel:userinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['personnel:userinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <!--
            <el-form-item label="用户昵称" prop="userinfoId">
              <el-select v-model="form.userinfoId" filterable placeholder="请选择用户昵称">
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.userName"
                  :value="parseInt(item.userId)"
                ></el-option>
              </el-select>
            </el-form-item>
            -->
            <el-form-item label="微信号" prop="chatNum">
              <el-input v-model="form.chatNum" placeholder="请输入微信号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预计转正时间" prop="planTime">
              <el-date-picker clearable size="small"
                v-model="form.planTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择预计转正时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证地址" prop="idAddr">
              <el-input v-model="form.idAddr" placeholder="请输入身份证地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="现居住地址" prop="liveAddr">
              <el-input v-model="form.liveAddr" placeholder="请输入现居住地址" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生年月" prop="birthday">
              <el-date-picker clearable size="small"
                v-model="form.birthday"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择出生年月">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="毕业院校" prop="school">
              <el-input v-model="form.school" placeholder="请输入毕业院校" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学历" prop="education">
              <el-select v-model="form.education" placeholder="请选择学历">
                <el-option
                  v-for="dict in dict.type.personnel_education"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="专业" prop="profession">
              <el-input v-model="form.profession" placeholder="请输入专业" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="婚姻状况" prop="marry">
              <el-select v-model="form.marry" placeholder="请选择婚姻状况">
                <el-option
                  v-for="dict in dict.type.personnel_userinfo_marry"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="紧急联系人" prop="ice">
              <el-input v-model="form.ice" placeholder="请输入紧急联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人关系" prop="iceRelat">
              <el-select v-model="form.iceRelat" placeholder="请选择联系人关系">
                <el-option
                  v-for="dict in dict.type.personnel_relat"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="联系人联系方式" prop="iceNum">
          <el-input v-model="form.iceNum" placeholder="请输入联系人联系方式" />
        </el-form-item>
        <el-form-item label="对公司的看重" prop="attitude">
          <el-input v-model="form.attitude" type="textarea" placeholder="请输入对公司的看重" />
        </el-form-item>
        <el-form-item label="职业规划" prop="planning">
          <el-input v-model="form.planning" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserinfo, getUserinfo, delUserinfo, addUserinfo, updateUserinfo } from "@/api/personnel/userinfo";
import {listUser} from "@/api/system/user";
import {validateSfz } from "@/api/personnel/IdCard";


export default {
  name: "Userinfo",
  dicts: ['personnel_userinfo_marry', 'personnel_education', 'personnel_relat'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工信息管理表格数据
      userinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 微信号时间范围
      daterangePlanTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userinfoId: null,
        planTime: null,
        education: null,
        planning: null,
        chatNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userinfoId: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        planTime: [
          { required: true, message: "预计转正时间不能为空", trigger: "blur" }
        ],
        idCard: [
          {  validator: validateSfz, trigger: "blur" }
        ],
        idAddr: [
          { required: true, message: "身份证地址不能为空", trigger: "blur" }
        ],
        liveAddr: [
          { required: true, message: "现居住地址不能为空", trigger: "blur" }
        ],
        birthday: [
          { required: true, message: "出生年月不能为空", trigger: "blur" }
        ],
        school: [
          { required: true, message: "毕业院校不能为空", trigger: "blur" }
        ],
        education: [
          { required: true, message: "学历不能为空", trigger: "change" }
        ],
        profession: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        marry: [
          { required: true, message: "婚姻状况不能为空", trigger: "change" }
        ],
        ice: [
          { required: true, message: "紧急联系人不能为空", trigger: "blur" }
        ],
        iceRelat: [
          { required: true, message: "联系人关系不能为空", trigger: "blur" }
        ],
        iceNum: [
          { required: true, message: "联系人联系方式不能为空", trigger: "blur" }
        ],
        attitude: [
          { required: true, message: "对公司的看重不能为空", trigger: "blur" }
        ],
        planning: [
          { required: true, message: "职业规划不能为空", trigger: "blur" }
        ],
        chatNum: [
          { required: true, message: "微信号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工信息管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePlanTime && '' != this.daterangePlanTime) {
        this.queryParams.params["beginPlanTime"] = this.daterangePlanTime[0];
        this.queryParams.params["endPlanTime"] = this.daterangePlanTime[1];
      }
      listUserinfo(this.queryParams).then(response => {
        this.userinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.userList = response.rows;
        this.total1 = response.total;
      });


    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userinfoId: null,
        planTime: null,
        idCard: null,
        idAddr: null,
        liveAddr: null,
        birthday: null,
        school: null,
        education: null,
        profession: null,
        marry: null,
        ice: null,
        iceRelat: null,
        iceNum: null,
        attitude: null,
        planning: null,
        chatNum: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePlanTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工信息管理编号为"' + ids + '"的数据项？').then(function() {
        return delUserinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('personnel/userinfo/export', {
        ...this.queryParams
      }, `userinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
